﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using System.Collections.ObjectModel;
using libConn;

namespace ApartmentSale
{
	public partial class Manager : Window
	{
		public Manager()
		{
			this.InitializeComponent();
		}

        private bool getData()
        {
            string errMessage = null;
            string str = "Select * From manager";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "An error occurred while reading the manager discounts",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            ObservableCollection<ManagerView> manager = new ObservableCollection<ManagerView>();

            foreach (DataRow r in oleDT.Rows)
            {
                manager.Add(new ManagerView()
                {
                    manager_id = Convert.ToInt32(r[0]),
                    manager_name = r[1].ToString().Replace("''", "'"),
                    manager_rebate_rate = Convert.ToDouble(r[2])
                });
            }

            dataGrid.DataContext = manager;

            return true;
        }

        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            ManagerInput dialog = new ManagerInput();
            dialog.ShowDialog();
            getData();
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid.SelectedIndex == -1)
                return;

            var manager = dataGrid.SelectedItem as ManagerView;
            if (manager != null)
            {
                if (MessageBox.Show("Sure you want to delete the manager discount? \nThis action can not be recovered!",
                    "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    return;

                string errMessage = null;
                string str = "DELETE FROM manager WHERE manager_id = " + manager.manager_id.ToString();

                libConn.AccdbConn.exec(str, ref errMessage);

                if (errMessage != null)
                {
                    System.Windows.MessageBox.Show(
                        errMessage,
                        "Remove managers off failure",
                        System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Error);
                    return;
                }

                getData();
            }
        }

        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btModify_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid.SelectedIndex == -1)
                return;

            var manager = dataGrid.SelectedItem as ManagerView;
            if (manager != null)
            {
                ManagerInput dialog = new ManagerInput(manager);
                dialog.ShowDialog();
                getData();
            }
        }

        private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (dataGrid.SelectedIndex == -1)
                return;

            var manager = dataGrid.SelectedItem as ManagerView;
            if (manager != null)
            {
                ManagerInput dialog = new ManagerInput(manager);
                dialog.ShowDialog();
                getData();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!getData())
                this.Close();
        }
	}
}